package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinStatisticsDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserCheckinCommand;
import com.cskaoyan.wordmemorize.service.UserCheckinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * @author 顾海龙
 * @version 1.0
 */
@RestController
@ResponseBody
public class UserSignController {

    @Autowired
    UserCheckinService userCheckinService;

    //用户签到  @RequestBody UserCheckinCommand userCheckinCommand
    @PostMapping("/user/checkin")
    public Result userSign(){
        UserCheckinCommand userCheckinCommand1 = new UserCheckinCommand();
        userCheckinCommand1.setType(1);
        userCheckinCommand1.setSignDate(LocalDate.now());
        userCheckinCommand1.setUserId(String.valueOf(StpKit.USER.getLoginIdAsLong()));
        UserCheckinStatisticsDTO checkin = userCheckinService.checkin(userCheckinCommand1);
        return Result.ok(checkin);
    }

    //补签
    @PutMapping("/user/checkin/card")
    public Result reSign(){

        return Result.ok();
    }

    //获取打卡数据
    @GetMapping("/user/checkin/statistics")
    public Result getCheckinList(){
        UserCheckinStatisticsDTO userCheckinStatistics = null;
        try {
            userCheckinStatistics = userCheckinService.getUserCheckinStatistics(StpKit.USER.getLoginIdAsLong());
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(userCheckinStatistics);
    }

    //获取下载的数据
    @GetMapping("/user/checkin/list")
    public Result getDownLoad(@ModelAttribute PageRequest pageRequest){
        PageDTO<UserCheckinDTO> checkInData = null;
        try {
            checkInData = userCheckinService.getCheckInData(pageRequest, StpKit.USER.getLoginIdAsLong());
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(checkInData);
    }
}
